﻿#region

using System;
using Core.Common;

#endregion

namespace Core.SQLBuilder
{
    public class SQLClause
    {
        private string key;
        private string op;
        private object val;

        public SQLClause(string key, object val, string op)
        {
            this.key = key;
            this.val = val;
            this.op = op;
        }

        public string Key
        {
            get { return key; }
            set { key = value; }
        }

        public object Val
        {
            get { return val; }
            set { val = value; }
        }

        public string Op
        {
            get { return op; }
            set { op = value; }
        }

        public string ToSQL()
        {
            if (val.GetType() == typeof (DateTime))
            {
                var d = (DateTime) val;
                val = "DATETIME'" + d.ToString("yyyy-MM-dd 00:00:00") + "'";
            }
            else if (val.GetType() == typeof (string))
            {
                if (op == CompareOperator.LIKE)
                    val = "%" + val + "%";
                val = "'" + val + "'";
            }
            return String.Format("{0} {1} {2}", key, op, val);
        }
    }
}